const config = require("./config.js");

/*********************** 对城市信息进行分组 ***************************/
var separationCitys = function cityList(searchLetter) {
  let tempArr = [];
  searchLetter.map(initial => {
    let tempObj = {};
    let cityInfo = [];
    tempObj.initial = initial;
    tempObj.cityInfo = cityObj.filter(city => city.initial == initial);

    tempArr.push(tempObj);
  });
  // console.log(JSON.stringify(tempArr));
  return tempArr;
};

const formatTime = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("-") +
    " " + [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
/*********************** 存储 ***************************/

/*********************** 弹框 ***************************/
// 显示繁忙提示
var showBusy = text =>
  wx.showToast({
    title: text,
    icon: "loading",
    duration: 10000
  });

// 显示成功提示
var showSuccess = text =>
  wx.showToast({
    title: text,
    icon: "success"
  });

var showToast = text =>
  wx.showToast({
    title: text || "缺少入参",
    icon: "none",
    duration: 2000
  });

// 自定义提示
var showModel = (title, content) => {
  wx.hideToast();
  return new Promise(function(resolve, reject) {
    wx.showModal({
      title,
      content: content,
      success(res) {
        if (res.confirm) {
          resolve(true)
        } else if (res.cancel) {
          resolve(false)
        }
      },
      showCancel: false
    });
  })



};

module.exports = {
  formatTime,
  showBusy,
  showSuccess,
  showToast,
  showModel,
  separationCitys
};